package com.smday.queue;

import java.util.Scanner;

/**
 * @author Summerday
 */
public class CircleArrayQueueDemo {

    public static void main(String[] args) {

        //创建一个环形队列,arrMaxSize为4,其实队列的有效数据最大是3
        CircleArrayQueue arrayQueue = new CircleArrayQueue(4);

        char key;
        Scanner scanner = new Scanner(System.in);

        boolean loop = true;

        while (loop) {

            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列取出数据");
            System.out.println("h(head):从队列查看头数据");
            key = scanner.next().charAt(0);

            switch (key) {

                case 's':
                    arrayQueue.showQueue();
                    break;

                case 'a':
                    System.out.println("输入一个数");
                    int i = scanner.nextInt();
                    arrayQueue.addQueue(i);
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.println(res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int head = arrayQueue.headQueue();
                        System.out.println(head);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;

                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }


        }
        System.out.println("程序退出!");


    }
}


class CircleArrayQueue {


    //队列最大容量
    private int maxSize;

    //指向队列的第一个元素,arr[front]是队列的第一个
    private int front;

    //指向队列尾的后一个,空出一个位置
    private int rear;

    //用于存放数据
    private int[] arr;

    public CircleArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = 0;
        rear = 0;
    }

    //判断队列是否满
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int n) {
        //判断队列是否满
        if (isFull()) {
            System.out.println("队列满!");
            return;
        }
        //直接将数据加入
        arr[rear] = n;
        //将rear后移,取模
        rear = (rear + 1) % maxSize;
    }

    //获取队列的数据,出队列
    public int getQueue() {
        //判断队列是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空!");
        }
        //此时front指向队列的第一个元素
        //先保存front对应的值

        int temp = arr[front];
        //将front后移
        front = (front + 1) % maxSize;
        //将临时的变量返回
        return temp;
    }

    //显示数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("数列为空");
            return;
        }

        //从front开始,只要不是最后一个的下一个,就让i循环移位
        for (int i = front; i != rear; i = (i + 1) % maxSize) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }

/*        //从front开始,遍历有效数据的个数,老师的思路
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }*/
    }

    //求当前队列的有效数据的个数
    public int size() {
        return (rear - front + maxSize) % maxSize;
    }

    //显示队列的头数据

    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空!");
        }
        return arr[front];
    }


}
